package cn.tedu.service.petcommodity.Impl;

import cn.tedu.commons.response.JsonPage;
import cn.tedu.mapper.petcategory.PetCategoryMapper;

import cn.tedu.pojo.pet.dto.PetCategoryDTO;
import cn.tedu.pojo.pet.entity.PetCategory;
import cn.tedu.pojo.pet.entity.PetType;
import cn.tedu.pojo.pet.vo.PetCategoryVO;

import cn.tedu.service.petcommodity.IPetCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class PetCategoryImpl implements IPetCategoryService {
    @Autowired(required = false)
    private PetCategoryMapper petCategoryMapper;

    @Override
    public void addCategory(PetCategoryDTO petCategoryDTO) {
        String categoryname=petCategoryDTO.getCategoryName();
        System.out.println(petCategoryDTO);
        PetCategory queryResult=petCategoryMapper.getByCategoryName(categoryname);
        System.out.println(queryResult);
        if (queryResult!=null){
            System.out.println("增加品种失败");
            throw new RuntimeException();
        }
        PetCategory petCategory=new PetCategory();
        petCategory.setCategoryName(categoryname);
        petCategory.setTypeId(petCategoryDTO.getTypeId());
        petCategory.setIsDelete(petCategoryDTO.getIsDelete());
        LocalDateTime now=LocalDateTime.now();
        petCategory.setGmtCreate(now);
        petCategory.setGmtModified(now);
        petCategoryMapper.addCategory(petCategory);
        System.out.println("添加宠物品种完成");

    }

    @Override
    public void updatePetCategory(String oldName,String newName) {
        PetCategory queryResult=petCategoryMapper.getByCategoryName(newName);
        if (queryResult!=null){
            System.out.println("修改品种失败，此品种已存在");
            throw new RuntimeException();
        }
        petCategoryMapper.updateCategory(oldName,newName,LocalDateTime.now());
        System.out.println("修改完成");


    }

    @Override
    public List<PetCategoryVO> getListByType(String typeName) {
        Long typeId= petCategoryMapper.getIdByName(typeName).getId();

        if (typeId==null){
            System.out.println("申请错误，不存在该宠物类型id");
            throw new RuntimeException("目标id没找到");
        }

        return petCategoryMapper.listTypeNameById(typeId);
    }

    @Override
    public void updateIsDelete(String categoryName, Integer isDelete) {
        PetCategory isExist= petCategoryMapper.getByCategoryName(categoryName);
        if (isExist==null){
            System.out.println("选择的宠物类别不存在");
            throw new RuntimeException("未能找到相应宠物类别");
        }
        petCategoryMapper.updateIsDelete(categoryName,isDelete);
        if (isDelete==1){
            System.out.println("修改成功！"+categoryName+"的状态为启用中");
        }else {
            System.out.println("修改成功！"+categoryName+"的状态为禁用中");
        }
    }

}
